/*jslint browser: true, devel: true*/
/*global $ window*/

/**
 ** 前端参数表
 **/
var CommonParameters = {


  // 统一权限参数读取
  authorityParamHost: window.CONFIG.restIp + '/v1/tenant/authorityparam/dataextrat',

  /**
   * 统一权限参数
   */
  getAuthorityParam: function (tenantId, successHandler, codeKey) {
    var dataExtractReqVo = {
      "dataType": "CfgParam",
      "filter": [
        {
          "name": "category",
          "type": 0,
          "value": codeKey
        }
      ],
      "tenantId": tenantId
    };
    var url = window.CONFIG.erip
    var getAuthorityParamRequest = $.ajax({
      url: url,
      type: "post",
      dataType: "json",
      data: JSON.stringify(dataExtractReqVo),
      timeout: 10000,
      async: true,
      beforeSend: function (request) {
        request.setRequestHeader("Content-Type", "application/json");
      },
      success: function (data) {
        successHandler(data);
      },
      error: function (XMLHttpRequest, status, errorThrown) {
        if ('timeout' != status) {
          console.log(new Date() + "[log info, 请求异常  " + status + " : " + errorThrown + "]");
        }
      },
      complete: function (XMLHttpRequest, status) {
        if ("timeout" === status) {
          console.log(new Date() + "[log info, 请求超时 : " + status + "]");
          getAuthorityParamRequest.abort();
          window.globalUiKit.notify({
            message: '请求超时，请检查网络情况！',
            type: 'warning',
            position: 'bottom-right',
            duration: 2000
          });
        }
      }
    });
  },

  /**
   * 统一权限参数返回值定义
   **/
  authRetCode: function () {
  },

  /**
   * 赋值
   **/
  codeShowHandler: function (codeKey, codeValue, langId) {
    langId = langId || 'zh_CH';
    if ('THEME' == codeKey) {
      var theme = CommonParameters.authRetCode['THEME|' + langId]
      window.Settings.theme = theme
      if(theme == 'theme0'){
        window.document.documentElement.setAttribute('data-theme', 'theme0')
      } else if(theme == 'theme1'){
        window.document.documentElement.setAttribute('data-theme', 'theme1')
      } else if(theme == 'theme2'){
        window.document.documentElement.setAttribute('data-theme', 'theme2')
      } else {
        window.document.documentElement.setAttribute('data-theme', 'theme0')
      }
    }
  },

  /**
   * 获取返回值内容
   *   codeKey：错误码值 必填项
   *   successHandler：获取后台成功回调
   *   langId：语种类型 可选，默认为中文
   **/
  getRetCodeValueByNo: function (codeKey, successHandler, tenantId, langId) {
    if (!codeKey) {
      return null;
    }

    langId = langId || 'zh_CH';
    var key = codeKey + "|" + langId;
    var value = CommonParameters.authRetCode[key];
    if (value) {
      if (successHandler) {
        successHandler(codeKey, value, langId);
      }
    } else {
      CommonParameters.getAuthorityParam(tenantId, function (data) {
        console.log('统一权限参数 :' + JSON.stringify(data));
        if (data) {
          if (data.info) {
            if (data.info.data) {
              if (data.info.data[0]) {
                CommonParameters.authRetCode[key] = data.info.data[0].codeValue;
                successHandler(codeKey, data.info.data[0].codeValue, langId);
              }
            }
          }
        }
      }, codeKey, langId);
    }
  }
};
